home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / HENSA / MATHS / PLPLOT / PLPLOT.ZIP / examples / C / x09c.c < prev    next >
C/C++ Source or Header  |  1994-06-30  |  4KB  |  165 lines

  1. /* $Id: x09c.c,v 1.10 1994/06/30 17:57:41 mjl Exp $
  2.  * $Log: x09c.c,v $
  3.  * Revision 1.10  1994/06/30  17:57:41  mjl
  4.  * All C example programs: made another pass to eliminate warnings when using
  5.  * gcc -Wall.  Lots of cleaning up: got rid of includes of math.h or string.h
  6.  * (now included by plplot.h), eliminated redundant casts, put in more
  7.  * uniform comments, and other minor changes.
  8.  *
  9.  * Revision 1.9  1994/03/30  07:21:53  mjl
  10.  * Changes to all C example programs: special handling for malloc re: header
  11.  * files eliminated, include of stdio.h and stdlib.h eliminated (now done
  12.  * by plplot.h), include of "plplot.h" changed to <plplot.h> to enable
  13.  * simpler builds by the general user, some cleaning up also.
  14. */
  15.  
  16. /*    x09c.c
  17.  
  18.     Contour plot demo.
  19. */
  20.  
  21. #include <plplot.h>
  22.  
  23. #define XPTS    35        /* Data points in x */
  24. #define YPTS    46        /* Datat points in y */
  25.  
  26. #define XSPA    2./(XPTS-1)
  27. #define YSPA    2./(YPTS-1)
  28.  
  29. static PLFLT clevel[11] =
  30. {-1., -.8, -.6, -.4, -.2, 0, .2, .4, .6, .8, 1.};
  31.  
  32. /* Utility macros */
  33.  
  34. #ifndef PI
  35. #define PI    3.1415926535897932384
  36. #endif
  37.  
  38. /* Transformation function */
  39.  
  40. PLFLT tr[6] =
  41. {XSPA, 0.0, -1.0, 0.0, YSPA, -1.0};
  42.  
  43. void
  44. mypltr(PLFLT x, PLFLT y, PLFLT *tx, PLFLT *ty, void *pltr_data)
  45. {
  46.     *tx = tr[0] * x + tr[1] * y + tr[2];
  47.     *ty = tr[3] * x + tr[4] * y + tr[5];
  48. }
  49.  
  50. /*----------------------------------------------------------------------*\
  51.  * main
  52.  *
  53.  * Does several contour plots using different coordinate mappings.
  54. \*----------------------------------------------------------------------*/
  55.  
  56. int
  57. main(int argc, char *argv[])
  58. {
  59.     int i, j;
  60.     PLFLT xx, yy, argx, argy, distort;
  61.     static PLINT mark = 1500, space = 1500;
  62.  
  63.     PLFLT **z, **w;
  64.     PLFLT xg1[XPTS], yg1[YPTS];
  65.     PLcGrid  cgrid1;
  66.     PLcGrid2 cgrid2;
  67.  
  68. /* Parse and process command line arguments */
  69.  
  70.     (void) plParseInternalOpts(&argc, argv, PL_PARSE_FULL);
  71.  
  72. /* Initialize plplot */
  73.  
  74.     plinit();
  75.  
  76. /* Set up function arrays */
  77.  
  78.     plAlloc2dGrid(&z, XPTS, YPTS);
  79.     plAlloc2dGrid(&w, XPTS, YPTS);
  80.  
  81.     for (i = 0; i < XPTS; i++) {
  82.     xx = (double) (i - (XPTS / 2)) / (double) (XPTS / 2);
  83.     for (j = 0; j < YPTS; j++) {
  84.         yy = (double) (j - (YPTS / 2)) / (double) (YPTS / 2) - 1.0;
  85.         z[i][j] = xx * xx - yy * yy;
  86.         w[i][j] = 2 * xx * yy;
  87.     }
  88.     }
  89.  
  90. /* Set up grids */
  91.  
  92.     cgrid1.xg = xg1;
  93.     cgrid1.yg = yg1;
  94.     cgrid1.nx = XPTS;
  95.     cgrid1.ny = YPTS;
  96.  
  97.     plAlloc2dGrid(&cgrid2.xg, XPTS, YPTS);
  98.     plAlloc2dGrid(&cgrid2.yg, XPTS, YPTS);
  99.     cgrid2.nx = XPTS;
  100.     cgrid2.ny = YPTS;
  101.  
  102.     for (i = 0; i < XPTS; i++) {
  103.     for (j = 0; j < YPTS; j++) {
  104.         mypltr((PLFLT) i, (PLFLT) j, &xx, &yy, NULL);
  105.  
  106.         argx = xx * PI/2;
  107.         argy = yy * PI/2;
  108.         distort = 0.4;
  109.  
  110.         cgrid1.xg[i] = xx + distort * cos(argx);
  111.         cgrid1.yg[j] = yy - distort * cos(argy);
  112.  
  113.         cgrid2.xg[i][j] = xx + distort * cos(argx) * cos(argy);
  114.         cgrid2.yg[i][j] = yy - distort * cos(argx) * cos(argy);
  115.     }
  116.     }
  117.  
  118. /* Plot using identity transform */
  119.  
  120.     plenv(-1.0, 1.0, -1.0, 1.0, 0, 0);
  121.     plcol(2);
  122.     plcont(z, XPTS, YPTS, 1, XPTS, 1, YPTS, clevel, 11, mypltr, NULL);
  123.     plstyl(1, &mark, &space);
  124.     plcol(3);
  125.     plcont(w, XPTS, YPTS, 1, XPTS, 1, YPTS, clevel, 11, mypltr, NULL);
  126.     plstyl(0, &mark, &space);
  127.     plcol(1);
  128.     pllab("X Coordinate", "Y Coordinate", "Streamlines of flow");
  129.  
  130. /* Plot using 1d coordinate transform */
  131.  
  132.     plenv(-1.0, 1.0, -1.0, 1.0, 0, 0);
  133.     plcol(2);
  134.     plcont(z, XPTS, YPTS, 1, XPTS, 1, YPTS, clevel, 11,
  135.        pltr1, (void *) &cgrid1);
  136.  
  137.     plstyl(1, &mark, &space);
  138.     plcol(3);
  139.     plcont(w, XPTS, YPTS, 1, XPTS, 1, YPTS, clevel, 11,
  140.        pltr1, (void *) &cgrid1);
  141.     plstyl(0, &mark, &space);
  142.     plcol(1);
  143.     pllab("X Coordinate", "Y Coordinate", "Streamlines of flow");
  144.  
  145. /* Plot using 2d coordinate transform */
  146.  
  147.     plenv(-1.0, 1.0, -1.0, 1.0, 0, 0);
  148.     plcol(2);
  149.     plcont(z, XPTS, YPTS, 1, XPTS, 1, YPTS, clevel, 11,
  150.        pltr2, (void *) &cgrid2);
  151.  
  152.     plstyl(1, &mark, &space);
  153.     plcol(3);
  154.     plcont(w, XPTS, YPTS, 1, XPTS, 1, YPTS, clevel, 11,
  155.        pltr2, (void *) &cgrid2);
  156.     plstyl(0, &mark, &space);
  157.     plcol(1);
  158.     pllab("X Coordinate", "Y Coordinate", "Streamlines of flow");
  159.  
  160.     plend();
  161.     free((void *) w);
  162.     free((void *) z);
  163.     exit(0);
  164. }
  165.